package com.gongpingjialoan.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.AppRecomendBean;
import com.gongpingjialoan.utility.BaseViewHolder;
import com.gongpingjialoan.utility.StepRecord;
import com.gongpingjialoan.view.GlideRoundTransform;

/**
 * Created by Administrator on 2016/4/20.
 */
public class AppRecomendAdapter extends BaseAdapter {

    private Context mContext;
    private AppRecomendBean mAppRecomendBean;


    public AppRecomendAdapter(Context context, AppRecomendBean appRecomendBeans) {
        mContext = context;
        mAppRecomendBean = appRecomendBeans;
    }

    @Override
    public int getCount() {
        if( mAppRecomendBean.getData() == null) return  0;
        return mAppRecomendBean.getData().size();
    }

    public void setData(AppRecomendBean appRecomendBeans){
        mAppRecomendBean = appRecomendBeans;
        notifyDataSetChanged();

    }


    @Override
    public Object getItem(int position) {
        if( mAppRecomendBean.getData() == null) return  null;
        return mAppRecomendBean.getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.app_item, null, false);
        }
        TextView tv1 = BaseViewHolder.get(convertView, R.id.text1);
        TextView tv2 = BaseViewHolder.get(convertView, R.id.text2);
        TextView download = BaseViewHolder.get(convertView, R.id.download);

        ImageView imageView = BaseViewHolder.get(convertView, R.id.image);
        final AppRecomendBean.DataEntity dataEntity = mAppRecomendBean.getData().get(position);
        tv1.setText(dataEntity.getName());
        tv2.setText(dataEntity.getDescription());
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StepRecord.recordStep(mContext , "downloadapp" , dataEntity.getApp_id()+"");
                Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(dataEntity.getUrl()));
                viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(viewIntent);
            }
        });
        Glide.with(mContext).load(dataEntity.getThumbnail()).transform(new GlideRoundTransform(mContext)).into(imageView);
        return convertView;
    }
}
